
package petrivis.view;

import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.geom.Area;
import java.awt.geom.Ellipse2D;
import petrivis.model.PetriNet;
import petrivis.view.PNRendererFactory.PlaceRenderer;
import prefuse.render.AbstractShapeRenderer;
import prefuse.render.ShapeRenderer;
import prefuse.visual.DecoratorItem;
import prefuse.visual.VisualItem;


/**
 * Renderer for the Places
 * @author Jorge Munoz
 */
public class TokenDecoratorRenderer extends AbstractShapeRenderer{

    private int baseSize = 10;

    private Area area1;
    private Ellipse2D   tok1 = new Ellipse2D.Double();
    private Area tok1Area;

    /**
     * Creates a new TokenDecoratorRenderer with default base size of 10 pixels.
     */
    public TokenDecoratorRenderer() {
    }

    /**
     * Creates a new TokenDecoratorRenderer with given base size.
     * @param size the base size in pixels
     */
    public TokenDecoratorRenderer(int size) {
       setBaseSize(size);
    }

    /**
     * Sets the base size, in pixels, for shapes drawn by this renderer. The
     * base size is the width and height value used when a VisualItem's size
     * value is 1. The base size is scaled by the item's size value to arrive
     * at the final scale used for rendering.
     * @param size the base size in pixels
     */
    public void setBaseSize(int size) {
        baseSize = size;
    }

    /**
     * Returns the base size, in pixels, for shapes drawn by this renderer.
     * @return the base size in pixels
     */
    public int getBaseSize() {
        return baseSize;
    }


    @Override
    protected Shape getRawShape(VisualItem item) {
        int marking = (Integer) item.get(PetriNet.MAR_COL);

        if(area1 == null){

            double x = item.getX();
            if ( Double.isNaN(x) || Double.isInfinite(x) )
                x = 0;
            double y = item.getY();
            if ( Double.isNaN(y) || Double.isInfinite(y) )
                y = 0;
            double width = baseSize*item.getSize();

            // Center the shape around the specified x and y
            if ( width > 1 ) {
                x = x-width/2;
                y = y-width/2;
            }

        }

        return area1;
    }

}
